$(function () {
    allGradeTable();
})


function query(params) {
    var param = {
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

function allGradeTable() {
    $("#grade_list").bootstrapTable({
        url:$.ip+"/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '年级名称',
                field: 'gradeName',
                align: 'center',
                valign: 'middle',

            },

            {
                title: '创建时间',
                align: 'center',
                field: 'createdAtStr',
                valign: 'middle',
            },
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="removeTr(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a> &nbsp<a onclick="update(this)" data-id="'+id+'" class="btn btn-warning " href="javascript:;">编辑</a>';
                }
            },
        ]
    });
}

function removeTr(obj){
    var id = $(obj).data("id");
    $.DemoGet("/grade/delete/"+id,function (data) {
        $("#grade_list").bootstrapTable('refresh');
    },function (e) {
        layer.msg("操作失败");
    });
}


function update(obj){
    var gradeId = $(obj).data("id");
    $.DemoGet("/grade/info/"+gradeId,function (data) {
        $("#gradeName").val(data.obj.gradeName);
        $("#gradeId").val(data.obj.id);
    },function (e) {
        layer.msg("操作失败");
    });
    popdiv("grade-info","修改",["50%","30%"],updateGrade);
}

function updateGrade() {
    var gradeName = $("#gradeName").val();
    var gradeId = $("#gradeId").val();
    $.DemoPost("/grade/update?token="+sessionStorage.getItem("token"),{"gradeName":gradeName,"id":gradeId},function (data) {
        layer.close(layer.index);
        layer.msg("修改成功");
        $("#grade_list").bootstrapTable('refresh');
    },function (error) {
        layer.msg("失败");
    })
}


function add(){
    $("#gradeName").val("");
    popdiv("grade-info","添加",["50%","30%"],addGrade);
}

function addGrade() {
    var gradeName = $("#gradeName").val().replace(/(^\s*)|(\s*$)/g, "");
    if(gradeName=='')
    {
        layer.msg("请输入年级名称");
        return;
    }
    $.DemoPost("/grade/add?token="+sessionStorage.getItem("token"),{"gradeName":gradeName},function (data) {
        layer.close(layer.index);
        layer.msg("添加成功");
        $("#grade_list").bootstrapTable('refresh');
    },function (error) {
        layer.msg("失败");
    })
}

$("#btnQuery").click(function () {
    $("#grade_list").bootstrapTable('refresh');

});